﻿//提交数据
function fromSubmit() {
    $("#btnOK").attr("disabled", "disabled");
    if (validator() == true) {
        var mydata = { id: $("#userID").val(), name: $("#userName").val(), pwd: $("#userPwd").val(), age: $("#UserAge").val(), phoneNumber: $("#UserphoneNumber").val(), sex: $("#userSex").val(), departname: $("#userDepartmentName").val(), address: $("#UserAddress").val() };
        $.post(
                "/Userinfo/AjaxAddUser",
                { userid: mydata.id, username: mydata.name, userpwd: mydata.pwd, userage: mydata.age, userphonenumber: mydata.phoneNumber, usersex: mydata.sex, userdepartname: mydata.departname, useraddress: mydata.address },
                function(date) {
                    if (date == "success") {
                        alert("添加员工信息成功！！");
                        form1.reset();
                        $("#new_userIDTip").text("");
                        $("#new_userNameTip").text("");
                        $("#btnOK").attr("disabled", "");
                    }
                    else if (date == "error") {
                        alert("添加员工信息失败！！");
                        $("#btnOK").attr("disabled", "");
                        window.location.href = "/Userinfo/ShowUser";
                    }
                    else {
                        alert(date);
                        $("#btnOK").attr("disabled", "");
                        window.location.href = "/Userinfo/ShowUser";
                    }
                })
    }
    else {
        alert("对不起！信息验证没有通过。请看具体的错误信息！");
        $("#btnOK").attr("disabled", "");
    }
}
//验证页面元素
function validator() {
    //判断用户编号在数据库是否存在而得到返回的值
    var new_userid_bool = null;
    var validator_new_userid = $("#validator_new_userid").val();
    if (validator_new_userid == "error") {
        new_userid_bool = false;
    }
    if (validator_new_userid == "success") {
        new_userid_bool = true;
    }
    else {
        new_userid_bool = false;
    }
    //判断用户姓名在数据库是否存在而得到返回的值
    var new_username_bool = null;
    var validator_new_username = $("#validator_new_username").val();
    if (validator_new_username == "error") {
        new_username_bool = false;
    }
    if (validator_new_username == "success") {
        new_username_bool = true;
    }
    else {
        new_username_bool = false;
    }
    //判断验证方法返回是true还是false；
    var bool = new_userid_bool && new_username_bool && validator_userpwd() && validator_new_userpwd() && validator_age() && validator_phoneNumber() && validator_sex() && validator_departmentname() && validator_address();
    return bool;
}
//验证用户编号
function validator_userid() {
    var user_id = $("#userID").val();
    if (user_id == "") {
        $("#userIDTip").text("用户编号不能为空！");
        return false;
    }
    else if (isNaN(user_id) == true) {
        $("#userIDTip").text("输入的用户编号只能是数字！");
        return false;
    }
    else {
        $.post(
                 "/Userinfo/AjaxuserIDValidator",
                 "userid=" + user_id,
                function(date) {
                    if (date == "error") {
                        $("#new_userIDTip").text("");
                        $("#validator_new_userid").val(date);
                        $("#userIDTip").text("用户编号已存在，请重新输入！");
                    } else {
                        $("#userIDTip").text("");
                        $("#validator_new_userid").val(date);
                        $("#new_userIDTip").text("用户编号可以使用！").css("color", "Green");
                    }
                });
    }
}
//验证用户姓名
function validator_username() {
    var user_name = $("#userName").val();
    if (user_name == "") {
        $("#userNameTip").text("输入用户姓名格式不合法！");
        return false;
    }
    else {
        $.post(
                 "/Userinfo/AjaxuserNameValidator",
                 "userName=" + user_name,
                 function(date) {
                     if (date == "error") {
                         $("#new_userNameTip").text("");
                         $("#validator_new_username").val(date);
                         $("#userNameTip").text("用户姓名已存在，请重新输入！");
                     } else {
                         $("#userNameTip").text("");
                         $("#validator_new_username").val(date);
                         $("#new_userNameTip").text("用户姓名可以使用！").css("color", "Green");
                     }
                 })
    }
}
//验证密码
function validator_userpwd() {
    //验证用户密码
    var user_pwd = $("#userPwd").val();
    if (user_pwd == "") {
        $("#userPwdTip").text("请输入用户的密码！");
        return false;
    }
    else if (user_pwd.length < 6) {
        $("#userPwdTip").text("输入的密码必须是6位或者是大于6位！");
        return false;
    }
    $("#userPwdTip").text("");
    return true;
}
//验证确认密码
function validator_new_userpwd() {
    var user_pwd = $("#userPwd").val();
    var new_user_pwd = $("#new_pwd").val();
    if (new_user_pwd == "") {
        $("#new_pwdTip").text("请输入用户的确认密码！");
        return false;
    }
    if (new_user_pwd != user_pwd) {
        $("#new_pwdTip").text("您输入的密码和确认密码不一致，清重新输入！");
        return false;
    }
    $("#new_pwdTip").text("");
    return true;
}
//验证年龄
function validator_age() {
    var intege1 = /^[0-9]{1,2}$/;
    var user_age = $("#UserAge").val();
    if (intege1.test(user_age) != true) {
        $("#UserAgeTip").text("您输入的年龄格式不合法！");
        return false;
    }
    if (user_age * 1 <= 1 * 0 || user_age * 1 > 1 * 90) {
        $("#UserAgeTip").text("年龄已经超出范围！");
        return false;
    }
    $("#UserAgeTip").text("");
    return true;
}

//验证电话
function validator_phoneNumber() {
    var user_phoneNumber = $("#UserphoneNumber").val();
    var mobile = /^(13|15)[0-9]{9}$/;
    if (mobile.test(user_phoneNumber) != true) {
        $("#UserphoneNumberTip").text("您输入的电话号码格式不合法！");
        return false;
    }
    $("#UserphoneNumberTip").text("");
    return true;
}
//验证性别
function validator_sex() {
    var user_sex = $("#userSex").val();
    if (user_sex == "0") {
        $("#userSexTip").text("性别必须选择！");
        return false;
    }
    $("#userSexTip").text("");
    return true;
}
//验证部门
function validator_departmentname() {
    var user_departmentname = $("#userDepartmentName").val();
    if (user_departmentname == "0") {
        $("#userDepartmentNameTip").text("部门必须选择！");
        return false;
    }
    $("#userDepartmentNameTip").text("");
    return true;
}
//验证地址
function validator_address() {
    var user_address = $("#UserAddress").val();
    if (user_address == "") {
        $("#UserAddressTip").text("您输入的地址格式不合法！");
        return false;
    }
    $("#UserAddressTip").text("");
    return true;
}